﻿using System;
using System.Linq;
using System.IO;

namespace QLab.Controllers.SolutionExplorer
{

	public class SolutionExplorerItemFolder : SolutionExplorerItemBase
	{
		public SolutionExplorerItemFolder()
			: base(SolutionExplorerItemType.Folder)
		{
#if DEBUG

			#region KeyOPS Test
			//// KeyOPS Test
			//ChildItems.Add(new SolutionExplorerItemFile() { Path = @"C:\Temp\QLib\TestProject_01\F1\file1.q" });
			//ChildItems.Add(new SolutionExplorerItemFile() { Path = @"C:\Temp\QLib\TestProject_01\F1\file2.q" });
			#endregion

#endif
		}

		public override void Load(string Path)
		{
			throw new System.NotImplementedException();
		}

		public override void Save()
		{
			try
			{
				// Create the directory if not exists.
				if (!System.IO.Directory.Exists(FullPath))
					System.IO.Directory.CreateDirectory(FullPath);
			}
			catch /* (System.Exception e) */
			{
				// Error message
			}
		}

		public override void Remove()
		{
			if (this.ChildItems != null && this.ChildItems.Count > 0)
			{
				foreach (SolutionExplorerItemBase child in ChildItems.ToArray())
					child.Remove();
			}

			DirectoryInfo di = new DirectoryInfo(FullPath);
			if (di.Exists)
			{
				try
				{
					di.Delete(/* recursive */ true);
				}
				catch (Exception e)
				{
					Console.WriteLine(e.ToString());
				}
			}

			if (Parent != null)
				Parent.ChildItems.Remove(this);
		}
	}
}
